package com.zdkj.subway.check.controller.subwayRat.conductorDifferences.SwAuConductorShort;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zdkj.subway.check.entity.SwAuCenterConductorAmountDifference;
import com.zdkj.subway.check.entity.SwAuConductorNotice;
import com.zdkj.subway.check.entity.SwAuConductorShort;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.SwAuCenterConductorAmountDifferenceMapper;
import com.zdkj.subway.check.mapper.SwAuConductorNoticeMapper;
import com.zdkj.subway.check.mapper.SwAuConductorShortMapper;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.SwAuConductorShortService;
import com.zdkj.subway.check.util.DateFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 线网层级确认-售票员票款差异确认-售票员补款确认通知
 */
@RestController
@RequestMapping("/checkNew")
@Slf4j
public class SwAuConductorShortCtrl {

    @Resource
    private SwAuConductorShortService swAuConductorShortService;
    @Resource
    SwAuConductorShortMapper swAuConductorShortMapper;
    @Resource
    SwAuCenterConductorAmountDifferenceMapper swAuCenterConductorAmountDifferenceMapper;
    @Resource
    SwAuConductorNoticeMapper swAuConductorNoticeMapper;
    /**
     * 售票员补款确认通知 列表
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuConductorShortList")
    @ApiOperation(value = "售票员补款确认通知 列表", notes = "售票员补款确认通知 列表")
    public Result getSwAuConductorShortList(@RequestBody JSONObject jsonObject){
        return swAuConductorShortService.getSwAuConductorNoticeList(jsonObject);
    }

    /**
     * 售票员补款确认通知 单独查询
     * @param jsonObject
     * @return
     */
    @RequestMapping("/getSwAuConductorShort")
    @ApiOperation(value = "售票员补款确认通知 单独查询", notes = "售票员补款确认通知 单独查询")
    public Result getSwAuConductorShort(@RequestBody JSONObject jsonObject){
        return swAuConductorShortService.getSwAuConductorNotice(jsonObject);
    }

    /**
     * 售票员补款确认通知 补款
     * @param jsonObject
     * @return
     */
    @RequestMapping("/buKuanSwAuConductorShort")
    @ApiOperation(value = "售票员补款确认通知 补款", notes = "售票员补款确认通知 补款")
    public Result buKuanSwAuConductorShort(@RequestBody JSONObject jsonObject){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            SwAuConductorShort swAuConductorShort = JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorShort.class);
            swAuConductorShort.setIsSupPayment(3);//0；未加入补款 1；未补款 3；已补款
            LocalDate supPaymentTimelimit = DateFormat.dateTOLocalDate(swAuConductorShort.getSupPaymentTimelimit());
            LocalDate newLocalDate = DateFormat.dateTOLocalDate(new Date());
            if(supPaymentTimelimit.compareTo(newLocalDate)>=0){
                swAuConductorShort.setIsLimitSuppay("1");
            }else{
                swAuConductorShort.setIsLimitSuppay("0");
            }
            if(swAuConductorShort.getSupPaymentTime()!=null){
                swAuConductorShort.setAccountTime(swAuConductorShort.getSupPaymentTime());//入账日期
            }
            swAuConductorShortMapper.updateById(swAuConductorShort);
            Integer diffId = swAuConductorShort.getDiffId();
            SwAuCenterConductorAmountDifference swAuCenterConductorAmountDifference = new SwAuCenterConductorAmountDifference();
            swAuCenterConductorAmountDifference.setId(diffId);
            swAuCenterConductorAmountDifference.setSuppState("3");//0；未加入补款 1；未补款 3；已补款
            swAuCenterConductorAmountDifferenceMapper.updateById(swAuCenterConductorAmountDifference);
            SwAuConductorNotice swAuConductorNotice = new SwAuConductorNotice();
            swAuConductorNotice.setIsSupPayment(3);//0；未加入补款 1；未补款 3；已补款
            UpdateWrapper<SwAuConductorNotice> objectUpdateWrapper = new UpdateWrapper<>();
            objectUpdateWrapper.eq("RELATIONID",diffId);
            swAuConductorNoticeMapper.update(swAuConductorNotice,objectUpdateWrapper);
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("补款失败");
        }
        return res;
    }


    /**
     * 售票员补款确认通知 批量补款
     * @param rows
     * @return
     */
    @RequestMapping("/batchBuKuanSwAuConductorShort")
    @ApiOperation(value = "售票员补款确认通知 批量补款", notes = "售票员补款确认通知 批量补款")
    public Result batchBuKuanSwAuConductorShort(String rows){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            JSONArray jsonArray=JSONArray.parseArray(rows);
            for(int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                SwAuConductorShort swAuConductorShort = JSONObject.parseObject(jsonObject.toJSONString(), SwAuConductorShort.class);
                swAuConductorShort.setIsSupPayment(1);
                if(swAuConductorShort.getSupPaymentTimelimit().compareTo(new Date())>=0){
                    swAuConductorShort.setIsLimitSuppay("1");
                }else{
                    swAuConductorShort.setIsLimitSuppay("0");
                }
                if(swAuConductorShort.getSupPaymentTime()!=null){
                    swAuConductorShort.setAccountTime(swAuConductorShort.getSupPaymentTime());//入账日期
                }
                swAuConductorShortMapper.updateById(swAuConductorShort);
            }

        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("补款失败");
        }
        return res;
    }


    /**
     * 售票员补款确认通知 确认
     * @param ids
     * @return
     */
    @RequestMapping("/querenSwAuConductorShort")
    @ApiOperation(value = "售票员补款确认通知 确认", notes = "售票员补款确认通知 确认")
    public Result querenSwAuConductorShort(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            List<SwAuConductorShort> swAuConductorShorts = swAuConductorShortMapper.selectBatchIds(Arrays.asList(ids.split(",")));
            for(SwAuConductorShort swAuConductorShort:swAuConductorShorts){
                swAuConductorShort.setConfirmStatus("1");
                swAuConductorShortMapper.updateById(swAuConductorShort);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("确认失败");
        }
        return res;
    }

}
